﻿using ED.Common.Extensions;
using System.Runtime.Serialization;

namespace ED.Common.BaseModel
{
    /// <summary>
    /// 响应数据输出
    /// </summary>
    /// <example>
    /// <code>
    /// public ResponseOutput&lt;Model> QueryModels()
    /// {
    /// 	//...
    /// 	return ResponseOutput.Ok(new Model() { });
    /// }
    /// </code>
    /// </example>
    [DataContract]
    public class ResponseOutput<T>
    {
        /// <summary>
        /// 是否成功标记
        /// </summary>
        [DataMember(Order = 1)]
        public bool Success { get; set; }

        /// <summary>
        /// 状态码
        /// </summary>
        [DataMember(Order = 2)]
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        [DataMember(Order = 3)]
        public string Msg { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        [DataMember(Order = 4)]
        public T Data { get; set; }

        /// <summary>
        /// 隐示转换
        /// </summary>
        /// <param name="res"></param>
        public static implicit operator ResponseOutput<T>(ResponseOutput res)
        {
            if (res == null)
            {
                return null;
            }

            return new ResponseOutput<T>()
            {
                Success = res.Success,
                Code = res.Code,
                Msg = res.Msg,
                Data = res.Data.To<T>()
            };
        }

        /// <summary>
        /// 隐示转换
        /// </summary>
        /// <param name="res"></param>
        public static implicit operator ResponseOutput(ResponseOutput<T> res)
        {
            if (res == null) return null;
            return new ResponseOutput()
            {
                Success = res.Success,
                Code = res.Code,
                Msg = res.Msg,
                Data = res.Data
            };
        }
    }

    /// <summary>
    /// 响应数据静态输出
    /// </summary>
    [DataContract]
    public class ResponseOutput : ResponseOutput<object>
    {

        #region 返回成功
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <returns></returns>
        public static ResponseOutput Ok()
        {
            return new ResponseOutput()
            {
                Success = true
            };
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ResponseOutput<T> Ok<T>(T data)
        {
            return new ResponseOutput<T>()
            {
                Success = true,
                Data = data
            };
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static ResponseOutput<T> Ok<T>(T data, string msg)
        {
            return new ResponseOutput<T>()
            {
                Success = true,
                Msg = msg,
                Data = data
            };
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ResponseOutput<T> Ok<T>(T data, int code)
        {
            return new ResponseOutput<T>()
            {
                Success = true,
                Code = code,
                Data = data
            };
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="msg">消息</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ResponseOutput<T> Ok<T>(T data, string msg, int code)
        {
            return new ResponseOutput<T>()
            {
                Success = true,
                Msg = msg,
                Code = code,
                Data = data
            };
        }
        #endregion

        #region 返回失败
        /// <summary>
        /// 返回失败
        /// </summary>
        /// <returns></returns>
        public static ResponseOutput NotOk()
        {
            return new ResponseOutput()
            {
                Success = false
            };
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static ResponseOutput NotOk(string msg)
        {
            return new ResponseOutput()
            {
                Success = false,
                Msg = msg
            };
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ResponseOutput NotOk(int code)
        {
            return new ResponseOutput()
            {
                Success = false,
                Code = code
            };
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ResponseOutput NotOk(string msg, int code)
        {
            return new ResponseOutput()
            {
                Success = false,
                Msg = msg,
                Code = code
            };
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ResponseOutput<T> NotOk<T>(string msg, T data)
        {
            return new ResponseOutput<T>()
            {
                Success = false,
                Msg = msg,
                Data = data
            };
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="code">状态码</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ResponseOutput<T> NotOk<T>(int code, T data)
        {
            return new ResponseOutput<T>()
            {
                Success = false,
                Code = code,
                Data = data
            };
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="code">状态码</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ResponseOutput<T> NotOk<T>(string msg, int code, T data)
        {
            return new ResponseOutput<T>()
            {
                Success = false,
                Msg = msg,
                Code = code,
                Data = data
            };
        }
        #endregion
    }
}
